package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.iwhalecloud.citybrain.flow.platform.manager.application.common.enums.FileHandleTypeEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.FlowBaseDataHandleProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.FlowFileHandleService;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowModelFileResourceHandleVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FileHandleRepository;
import org.apache.commons.lang3.StringUtils;
import org.flowable.task.api.Task;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
　　* @description: 文件操作服务
　　* @author shenrm
　　* @date 2021/11/24 20:00
　　*/
@Service
public class FlowModelFileHandleServiceImpl implements FlowFileHandleService {

    @Autowired
    private FileHandleRepository fileHandleRepository;

    @Autowired
    private FlowBaseDataHandleProcessor flowBaseDataHandleProcessor;

    @Override
    public void batchInsertFileResourceHandleRel(List<FlowModelFileResourceHandleVo> handleVos) {
        fileHandleRepository.batchInsertFileResources(handleVos);
    }

    @Override
    public void updateByExampleSelective(FlowModelFileResourceHandleVo handleRelVo) {
        fileHandleRepository.updateByExampleSelective(handleRelVo);
    }

    /**
     * 根据条件查询
     *
     * @param handleRelVo
     * @return
     */
    @Override
    public List<FlowModelFileResourceHandleVo> selectByCondition(FlowModelFileResourceHandleVo handleRelVo) {
        if (Objects.isNull(handleRelVo)) {
            return Collections.emptyList();
        }

        // 流程实例
        String deploymentId = flowBaseDataHandleProcessor.getDeploymentId(handleRelVo.getProcessInstanceId());
        if (StringUtils.isBlank(deploymentId)) {
            return Collections.emptyList();
        }

        Task task = null;
        if(StringUtils.isNotBlank(handleRelVo.getProcessInstanceId()) &&
                StringUtils.isNotBlank(handleRelVo.getTaskId())) {
            // 根据流程实例ID和任务ID获取任务实例对象
            task = flowBaseDataHandleProcessor.resolveHistoryTaskByPidAndTid(handleRelVo.getProcessInstanceId(), handleRelVo.getTaskId());
            if (Objects.isNull(task)) {
                return Collections.emptyList();
            }
        }

        // 强制转换成任务实体
        TaskEntity taskEntity = null;
        if (Objects.nonNull(task) && Objects.equals(handleRelVo.getServiceType(), FileHandleTypeEnum.FLOWACTIVITYMODEL.getCode())) {
            taskEntity = (TaskEntity) task;
            // 获取节点编码
            String activityId = taskEntity.getTaskDefinitionKey();
            // 节点编码
            handleRelVo.setActivityId(activityId);
        }

        // 部署ID
        handleRelVo.setDeploymentId(deploymentId);
        return fileHandleRepository.selectByCondition(handleRelVo);
    }

    @Override
    public List<FlowModelFileResourceHandleVo> selectByAttachIds(List<FlowModelFileResourceHandleVo> vos) {
        if(CollectionUtils.isEmpty(vos)) {
            return Collections.emptyList();
        }
        List<String> attachIds = new ArrayList<>();
        for(FlowModelFileResourceHandleVo vo:vos) {
            attachIds.add(vo.getAttachId());
        }
        return fileHandleRepository.selectByAttachIds(attachIds);
    }
}
